import $ from '../jquery-3.4.1.min.js';
import ajax from '../utils/ajax';

renderJoinPack();
//入库返回
$('.addBuyer #back').click(function () {
    $('.target').addClass("active");
    $('.buyer').removeClass("active");
})
//入库保存
$(".addBuyer #save").click(function () {
    addJoinPack();
})
//删除
$("#joinPackTbody").on('click', '.joinPackOut', function () {
    const isRun = confirm('是否删除');
    if (isRun) {
        const _id = $(this).attr("index");
        const _id1 = $(this).attr("index1")
        outJoinPack(_id, _id1);
    }
})


//功能区
function addJoinPack() {
    const joinPackTime = $('#dateInput').val();
    const joinPackId = $("#joinNum").attr("index");
    const joinPackName = $("#packSelect").val();
    const joinPackMaterial = $("#materialSelect").val();
    const joinPackNote = $('#joinNote').val();
    const arrId = document.querySelectorAll("#buyerTbody .thingDel");
    let thingList_idArr = [];
    if (arrId.length > 0) {
        for (let i = 0; i < arrId.length; i++) {
            thingList_idArr.push(arrId[i].getAttribute("index"))
        }
        ajax({
            url: '/joinPack/addJoinPack',
            type: 'post',
            data: {
                joinPackTime, joinPackId,
                joinPackName, joinPackMaterial,
                joinPackNote, thingListId: thingList_idArr,
                joinPacKState: false
            },
            success(res) {
                if (res.code == 200) {
                    alert("入库成功");
                    $('#dateInput').val('');
                    $("#packSelect").val('');
                    $("#materialSelect").val('');
                    $('#joinNote').val('');
                    $('.target').addClass("active");
                    $('.buyer').removeClass("active");
                    renderJoinPack();
                } else {
                    alert("入库失败");
                }
            }
        })

    }

}
//获取入库数据
function getJoinPack() {
    return new Promise((resolve, reject) => {
        ajax({
            url: '/joinPack/getJoinPack',
            type: 'post',
            success(res) {
                resolve(res);
            }
        })
    })

}
//渲染入库数据
async function renderJoinPack() {
    let html = '';
    const { result } = await getJoinPack();
    console.log('入库的数据',result);
    for (let i = 0; i < result.length; i++) {
        if (result[i].thingListId.length > 0) {
            const thingList = result[i].thingListId;
            for (let k = 0; k < thingList.length; k++) {
                if (thingList[k].thingState1 == false) {
                    html += result[i].thingListId.map((item, index) =>
                        `<tr>
                <td claas='joinPackId'>${result[i].joinPackId}</td>
                <td class='joinPackState'>入库</td>
                <td class='joinPackAdmin'>周维维</td>
                <td class='joinPackTime'>${result[i].joinPackTime}</td>
                <td class='joinPackMaterial'>${result[i].joinPackMaterial}</td>
                <td class='joinPackName'>${result[i].joinPackName}</td>
                <td class='joinPackthingName'>${item.thingName}</td>
                <td class='joinPackthingBrand'>${item.thingBrand}</td>
                <td class='joinPackSet'><span class='spans joinPackOut' index='${result[i]._id}' index1='${item._id}'>删除</span> <span class='spans joinPackSet' index='${result[i]._id}'>修改</span></td>
                </tr>
                `
                    ).join("");
                }
            }

        }
    }
    $("#joinPackTbody").html(html);
    //渲染出库里面的选项
    let html1 = '';
    for (let j = 0; j < result.length; j++) {
        if (result[j].thingListId.length > 0) {
            const thingList1 = result[j].thingListId;
            for (let l = 0; l < thingList1.length; l++) {
                if (thingList1[l].thingState1 == false) {
                    html1 += result[j].thingListId.map((item, index) =>
                        `<tr>
                <td class='joinPackInputTd'><input type='checkbox' index='${result[j]._id}' index1='${item._id}'></td>
                <td claas='joinPackId'>${result[j].joinPackId}</td>
                <td class='joinPackthingName'>${item.thingName}</td>
                <td class='joinPackMaterial'>${result[j].joinPackMaterial}</td>
                <td class='joinPackName'>${result[j].joinPackName}</td>
                <td class='joinPackthingBrand'>${item.thingBrand}</td>
                <td class='joinPackTime'>${result[j].joinPackTime}</td>
                </tr>
                `
                    ).join("");
                }
            }

        }
    }
    $("#outPackTbody").html(html1);

}
//删除
async function outJoinPack(_id, _id1) {
    ajax({
        url: '/joinPack/outJoinPack',
        type: 'post',
        data: {
            _id, _id1
        },
        success(res) {
            if (res.code == 200) {
                alert("删除成功");
                renderJoinPack();
            } else {
                alert('删除失败');
            }
        }
    })
}


